package fiuba.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Banner extends BaseObject{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2348191516077182885L;

	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	private Long id;
	private String nombre;
	private List <BannerItem> items  = new ArrayList<BannerItem>();
	
	
	@Id @GeneratedValue(strategy = GenerationType.AUTO) 
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	@Column(length=50, unique=true)
	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}
	@OneToMany(mappedBy="banner")
	public List<BannerItem> getItems() {
		return items;
	}

	public void setItems(List<BannerItem> items) {
		this.items = items;
	}
	
	public String generateHTML() {
		int cantidad = 0;
		int acumulado = 0;
		String ret = "";
		// Se obtiene las chances totales
		for (BannerItem bi : this.getItems()) {
			cantidad += bi.getPosibilidad();
		}
		if (cantidad == 0) return "";
//    	log.debug("Cantidad: " + cantidad);
		// Obtener un random entre 0 y las chances
    	int valor = new Double(Math.random() * cantidad).intValue();
    	// buscar por el acumulado
//    	log.debug("Valor elegido: " + valor);
    	for (BannerItem bi : this.getItems()) {
    		acumulado += bi.getPosibilidad();
//			log.debug("ACUMULADO: " + acumulado);
    		if (acumulado > valor) {
//    			log.debug("ENCONTRADO");
    			if (bi.getTipo() == 1) {
    				if (! bi.getLink().equals("")) {
    					ret += "<a href='" + bi.getLink() + "'";
    					if (bi.isNuevaVentana()) ret += " target='_blank'";
    					ret += " >";
    				}
    				ret += "<img src='" + bi.getUrl() + "'";
    				if (bi.getAlto() > 0) ret += " height='" + bi.getAlto() + "'";
    				if (bi.getAncho() > 0) ret += " width='" + bi.getAncho() + "'";
    				ret += ">";
    				if (! bi.getLink().equals("")) {
    					ret += "</a>";
    				}
    			}
    			else {
    				String strSize = "";
    				if (bi.getAlto() > 0) strSize += " height='" + bi.getAlto() + "'";
    				if (bi.getAncho() > 0) strSize += " width='" + bi.getAncho() + "'";
    				
    				ret += "<object codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' " + strSize + ">"
    				+ "<param value='" + bi.getUrl() + "' name='movie'>"
    				+ "<param value='high' name='quality'>"
    				+ "<param value='false' name='menu'>"
    				+ "<param name='wmode'>"
    				+ "<embed type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' menu='false' quality='high' src='" + bi.getUrl() + "' " + strSize + ">"
					+ "</object>";

    			}
    			break;
    		}
		}
    	
		
		
		return ret;
	}

}
